/*
 * VHClubSeasonSummaryGrid.java
 * Created on 22 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.HeaderGroupConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.res.VHResConstants;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Represents a grid that displays club season/summary records.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHClubSeasonSummaryGrid extends LayoutContainer implements VHRpcResultReceiver<List<VHJsData>>
{
  private final ListStore<VHJsData> d_store;
  private Grid<VHJsData>            d_grid;

  public VHClubSeasonSummaryGrid()
  {
    d_store = new ListStore<VHJsData>();

    buildGrid();
    setLayout( new FitLayout() );
    add( d_grid );
  }

  public void setRpcData( List<VHJsData> result, String strPurpose )
  {
    d_store.removeAll();
    d_store.add( result );
    d_store.sort( "season_name", SortDir.DESC );
  }

  private void buildGrid()
  {
    GridCellRenderer<VHJsData> renderer = new GridCellRenderer<VHJsData>() {
      public String render( VHJsData data, String strProperty, ColumnData config, int rowIndex, int colIndex,
          ListStore<VHJsData> store, Grid<VHJsData> grid )
      {
        if ( strProperty.endsWith( "Rank" ) )
        {
          int nVal = data.getInt( strProperty );
          if ( nVal == 0 )
            return "-";
          else if ( nVal == 1 )
            return "<span style='font-weight: bold; font-style: italic; color: red;'>" + nVal + "</span>";
          else
            return "<span style='font-weight: bold; font-style: italic;'>" + nVal + "</span>";
        }

        return String.valueOf( data.get( strProperty ) );
      }
    };

    // Column definitions
    //
    VHResConstants constants = VHMain.getConstants();
    ArrayList<ColumnConfig> listCols = new ArrayList<ColumnConfig>();
    ColumnConfig col;

    RowNumberer rowNumberer = new RowNumberer();
    listCols.add( rowNumberer );

    col = new ColumnConfig();
    col.setId( "season_name" );
    col.setHeader( constants.season() );
    col.setWidth( 70 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "eCRRank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 30 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "eCRPoints" );
    col.setHeader( constants.numPointsAbbrev() );
    col.setToolTip( constants.numPoints() );
    col.setWidth( 40 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setNumberFormat( NumberFormat.getFormat( "0.00" ) );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "leagueRank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 30 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "leagueCategory" );
    col.setHeader( constants.leagueCategoryAbbrev() );
    col.setToolTip( constants.leagueCategory() );
    col.setWidth( 28 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "leagueRankType_translated" );
    col.setHeader( constants.rankType() );
    col.setWidth( 110 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "cupRank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 30 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "cupReachedRound_translated" );
    col.setHeader( constants.reachedRound() );
    col.setWidth( 110 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "cupWonRounds" );
    col.setHeader( constants.numWonKoSeriesAbbrev() );
    col.setToolTip( constants.numWonKoSeries() );
    col.setWidth( 28 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "cLRank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 30 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "cLReachedRound_translated" );
    col.setHeader( constants.reachedRound() );
    col.setWidth( 110 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "wLRank" );
    col.setHeader( constants.rankAbbrev() );
    col.setToolTip( constants.rank() );
    col.setWidth( 30 );
    col.setAlignment( HorizontalAlignment.RIGHT );
    col.setResizable( true );
    col.setRenderer( renderer );
    listCols.add( col );

    col = new ColumnConfig();
    col.setId( "wLReachedRound_translated" );
    col.setHeader( constants.reachedRound() );
    col.setWidth( 110 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    listCols.add( col );

    final ColumnModel cm = new ColumnModel( listCols );

    cm.addHeaderGroup( 0, 2, new HeaderGroupConfig( constants.navMainECRAbbrev(), 1, 2 ) );
    cm.addHeaderGroup( 0, 4, new HeaderGroupConfig( constants.league(), 1, 3 ) );
    cm.addHeaderGroup( 0, 7, new HeaderGroupConfig( constants.cup(), 1, 3 ) );
    cm.addHeaderGroup( 0, 10, new HeaderGroupConfig( constants.championsLeague(), 1, 2 ) );
    cm.addHeaderGroup( 0, 12, new HeaderGroupConfig( constants.worldLeague(), 1, 2 ) );

    // Grid
    //
    d_grid = new Grid<VHJsData>( d_store, cm );
    d_grid.addPlugin( rowNumberer );
    d_grid.setStripeRows( true );
    d_grid.setBorders( true );
    d_grid.setAutoWidth( true );
  }
}
